Fix: Main thread blocked for ~1s during WebRTC connection setup #834
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🎯 Goal
When joining a call, especially the first call after app launch, the UI freezes for approximately 1 second. This creates a poor user experience with an unresponsive interface.
📝 Summary
Root Cause
setLocalDescription
andsetRemoteDescription
are marked with@MainActor
Instrumentation Graphs
See here there is a 1 second hang in instrumentation:

WebRTC Code Trace
setLocalDescription
is defined inRTCPeerConnection
: https://github.com/GetStream/webrtc/blob/main/sdk/objc/api/peerconnection/RTCPeerConnection.mm#L595-L601_peerConnection
is artc::scoped_refptr<webrtc::PeerConnectionInterface>
, with it's concreteSetLocalDescription
defined here: https://github.com/GetStream/webrtc/blob/main/pc/peer_connection.cc#L1494-L1499sdp_handler_
is aSdpOfferAnswerHandler
, with it'sSetLocalDescription
defined here: https://github.com/GetStream/webrtc/blob/main/pc/sdp_offer_answer.cc#L1555-L1580DoSetLocalDescription
(https://github.com/GetStream/webrtc/blob/main/pc/sdp_offer_answer.cc#L1542C24-L1545) which then invokesPushdownTransportDescription
(https://github.com/GetStream/webrtc/blob/main/pc/sdp_offer_answer.cc#L1696)PushdownTransportDescription
finally callsSetLocalDescription
on thetransport_controller_s
which is aJsepTransportController
: https://github.com/GetStream/webrtc/blob/main/pc/sdp_offer_answer.cc#L4909-L4910🛠 Implementation
@MainActor
from both methods to allow them to run on any threadWhy Queue Serialization is Necessary
Removing
@MainActor
alone introduces race conditions when multiple threads access the RTCPeerConnection simultaneously. Manual testing showed:The queue maintains the thread safety that
@MainActor
provided, but without blocking the main thread.🧪 Manual Testing Notes
Testing Results (10 attempts each):
☑️ Contributor Checklist